package ch.fhnw.qualiscan.application.pages.tabs;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.components.InformationBox;
import ch.fhnw.qualiscan.application.pages.NewSurvey;
import ch.fhnw.qualiscan.core.db.fieldfactories.SurveyFieldFactory;
import ch.fhnw.qualiscan.core.db.pojos.Domain;
import ch.fhnw.qualiscan.core.db.pojos.Stage;
import ch.fhnw.qualiscan.core.db.pojos.Survey;

import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;

import eu.livotov.tpt.i18n.TM;

@SuppressWarnings("serial")
public class NewSurveyTab1 extends VerticalLayout {
	
	private NewSurvey newSurvey;
	public Form generalInfoForm;
	
	public NewSurveyTab1(NewSurvey newSurvey) {
		setMargin(true);
		
		this.newSurvey = newSurvey;
		generalInfoForm = new Form();
        
		// title
		addComponent(newSurvey.textHelper.h3(TM.get("newsurvey.tab1.general.title")));
		
		// helper box
		addComponent(new InformationBox());
		
		// form
		buildGeneralInfoForm();
        this.addComponent(generalInfoForm);
        
	}
	/**
	 * Builds the a form with fields for general information about the survey (title, domains, completion date and category). 
	 */
	private void buildGeneralInfoForm() {
		
        generalInfoForm.setDescription(TM.get("newsurvey.tab1.general.desc"));
        
        newSurvey.survey = new Survey();
        newSurvey.survey.setCreationdate(new java.util.Date());
        Stage firstStage = new Stage();
        firstStage.setId(1);
        newSurvey.survey.setStage(firstStage);
        
        BeanItem<Survey> item = new BeanItem<Survey>(newSurvey.survey);
        
        generalInfoForm.setItemDataSource(item);
        generalInfoForm.setImmediate(true);
        
        generalInfoForm.setFormFieldFactory(new SurveyFieldFactory());
        
        Vector<String> order = new Vector<String>();
        order.add("title");
        order.add("domains");
        order.add("completiondate");
        order.add("category");
        order.add("university");
        
        generalInfoForm.setVisibleItemProperties(order);
        HorizontalLayout buttonBar = new HorizontalLayout();
        generalInfoForm.getFooter().addComponent(buttonBar);
        
        //next step button
        Button commit = new Button(TM.get("action.next"), new ClickListener() {
                public void buttonClick(ClickEvent event) {
                        try {
                                generalInfoForm.commit();
                                Set s = newSurvey.survey.getDomains();
                                newSurvey.survey.setDomains(new HashSet());
                                Iterator i = s.iterator();
                                while (i.hasNext()) {
                                        Domain d = new Domain();
                                        d.setId((Integer)i.next());
                                        newSurvey.survey.addDomain(d);
                                }
                                newSurvey.tab2.setEnabled(true);
                                NewSurveyTab2 t2 = (NewSurveyTab2) newSurvey.TabComponent2;
                                t2.buildWeighing();
                                
                                newSurvey.tabSheet.setSelectedTab(newSurvey.TabComponent2);
                                newSurvey.tab1.setEnabled(false);
                        }
                        catch (InvalidValueException e) {
                                return;
                        }
                }
        });
        commit.setIcon(Config.ICON_GO_TAB);
        buttonBar.addComponent(commit); 	
	}

}
